import store from '@/store'
import { loginByUsername, logout, getUserInfo } from '@/api/login'
//import websocketApi from '@/api/websocket'
//import { Notification } from 'element-ui';

var websocketApi= process.env.NODE_ENV === 'development' ? require('config/dev.env'):process.env.ENV_CONFIG==='sit'?require('config/sit.env'):require('config/prod.env')
websocketApi=eval(websocketApi.SOCKET_API);

var websocket  = new WebSocket(websocketApi+store.getters.token);

websocket.onmessage = function onMessage(evt) {
  var data = JSON.parse(evt.data);
  if(data.message){
    const h = eventHub.$createElement;
    VM.$notify({
      title: '消息提示',
      message: h('i', { style: 'color: teal'}, data.message),
      duration:30000
    });
    websocket.send(JSON.stringify('更新消息'));
  }
  store.dispatch('SetIsdot',data.notificationCount);
};
websocket.onclose = function onClose() {
  //console.log("断开")
  //logout()
  return true
};
export default websocket;
export function closeHomeSocket(){
  if(websocket){
     websocket.close();
  }
};
export function sendHomeSocket(){
  if(websocket){
    websocket.send(JSON.stringify('更新消息'));
  }
};
websocket.onopen = function onOpen() {
    websocket.send(JSON.stringify('连接成功'));
};
websocket.onerror = function onError() {
    console.log("连接服务器失败");
};